/**
 * 2017年3月9日
 */
package cn.edu.bjtu.alex.rewrite.components;

import java.io.PrintWriter;
import java.util.Map;
import java.util.Map.Entry;

import cn.edu.bjtu.alex.rewrite.Context;
import cn.edu.bjtu.alex.rewrite.interfaces.Term;
import cn.edu.bjtu.alex.rewrite.interfaces.TermFeatureable;

/**
 * @author Alex
 *
 */
public class TransformOutputingLibSVM extends SortSupport<Term> {
	Map<String,TermFeatureable> features = null;
	public TransformOutputingLibSVM(Context ctx) {
		super(ctx);
		// TODO Auto-generated constructor stub
	}
	@Override
	protected void fitInternal() {}

	/* (non-Javadoc)
	 * @see cn.edu.bjtu.alex.rewrite.components.BaseComponent#transformInternal()
	 */
	@Override
	protected void transformInternal() {
		// TODO Auto-generated method stub
		PrintWriter pw = new PrintWriter(ctx.getOs());
		Map<String,Map<String, Term>> map = ctx.getTransformedDocument();
		features = ctx.featuredTermsMap();
		
		for(Entry<String,Map<String, Term>> entry:map.entrySet()){
			pw.print(entry.getKey());
			pw.print(" ");
			Map<String, Term> m = entry.getValue();
			sortMap(m);			
			while(hasNext()){
				Entry<String,Term> word = next();
				pw.print(String.format("%d:%f ",features.get(word.getKey()).getId(),word.getValue().getTfidf()));
			}
			pw.println();
		}
		pw.flush();
		

	}

	@Override
	protected int sortBy(String key) {
		return features.get(key).getId();
	}

	@Override
	protected boolean shouldAdd(Entry<String, Term> entry) {
		return features.containsKey(entry.getKey());
	}

}
